﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MobileIdeas.MobileHealth.SDK.DAL.Model;

namespace MobileIdeas.MobileHealth.SDK.DAL.DataAccess
{
    public class FarmaciaDao : DataAccessObject
    {
        #region Get

        public Farmacia GetFarmacia(int farmaciaId)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    return entities.farmacia.Include("Pais").Include("Departamento").Include("Provincia").FirstOrDefault(e =>
                        e.farmaciaId == farmaciaId);
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        public List<Farmacia> GetFarmaciasFinanciador(int financiadorId)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    var query = from e in entities.farmacia
                                where (e.financiador.financiadorId == financiadorId)
                                select e;

                    return query.ToList();
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        #endregion

        #region Add

        public void AddFarmacia(Farmacia farmacia)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    entities.AddTofarmacia(farmacia);
                    this.SaveChanges(entities);
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        #endregion

        #region Modify

        public void ModifyFarmacia(Farmacia farmacia)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    Farmacia oldFarmacia = entities.farmacia.FirstOrDefault(e => e.farmaciaId == farmacia.farmaciaId);
                    oldFarmacia = farmacia;

                    this.SaveChanges(entities);
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        #endregion
    }
}
